package finalfinancialanalyst.gui;

import finalfinancialanalyst.business.Transaction;
import finalfinancialanalyst.business.Account;
import java.util.Date;
import javax.swing.table.AbstractTableModel;

public class TransactionModel extends AbstractTableModel { 
    
    private Account account;
    private String[] header = {
        "Name", "Category", "Date", "Amount", "Location", "Comment", "Barcode"
    };

    public TransactionModel(Account account) {
        this.account = account;
    }

    public Account getAccount() {
        return account;
    }

    @Override
    public int getRowCount() {
        return account.transactionCount();
    }

    @Override
    public int getColumnCount() {
        return header.length;
    }

    @Override
    public String getColumnName(int columnIndex) {
        return header[columnIndex];
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        switch (columnIndex) {
        case 2:
            return Date.class;
        default:
            return String.class;
        }
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return false;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Transaction tra = account.getTransaction(rowIndex);
        switch (columnIndex) {
            
            case -1:
                return tra;            
            case 0:
                return tra.getName();
            case 1:
                return tra.getCategory();
            case 2:
                return tra.getTime();
            case 3:
                String amount = tra.getAccountAmount().getAmount().toString();
                if (tra.getIsExpenditure()) { amount = "-" + amount; }
                return amount;
            case 4:
                return tra.getLocation().getName();
            case 5:
                return tra.getComment();
            case 6:
                return Long.toString(tra.getBarcode());
            default:
                return null;

        }
    }  
}
